package com.woniuxy.microfundservice.config;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.woniuxy.bean.ResultBean;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpResponse;

/**
 * @author: masterhai
 * @date: 2021/4/22:19:35
 * @description: sentinel全局异常处理熔断降级
 */

public class GlobeExceptionHandler {

    /**
     * 服务流量控制处理
     * @param request
     * @param body
     * @param execution
     * @param exception
     * @return
     */
    public static ClientHttpResponse limitRate(HttpRequest request,
                                               byte[] body, ClientHttpRequestExecution execution,
                                               BlockException exception) {
        exception.printStackTrace();
        return new SentinelClientHttpResponse(
                JSON.toJSONString(ResultBean.builder().code(10000).msg("[定期微服务]已达限流标准！").build()));
    }
    /**
     * 服务熔断降级处理
     * @param request
     * @param body
     * @param execution
     * @param exception
     * @return
     */
    public static ClientHttpResponse fallback(HttpRequest request,
                                              byte[] body,
                                              ClientHttpRequestExecution execution,
                                              BlockException exception) {
        exception.printStackTrace();
        return new SentinelClientHttpResponse(
                JSON.toJSONString(ResultBean.builder().code(10000).msg("[定期微服务]已达限流|熔断标准！").build()));
    }
}
